package cn.jja8.bukkit.bukkitcommandtool.command;

import org.bukkit.command.CommandSender;

import java.util.*;


public class CommandList extends Command implements NeedFirst{
    /**在列表里的命令*/
    List<Command> commands = new ArrayList<>();

    //需要初始化后才有数据

    /**用于匹配命令名称，全部大写匹配*/
    Map<String,Command> commandMap;
    /**用于补全命令*/
    List<String> commandStrings;

    public CommandList(String name) {
        super(new CommandListLanguage(),name);
    }


    @Override
    public boolean command(CommandSender commandSender, String[] args) {
        if (power!=null){
            if (!commandSender.hasPermission(power)) {
                commandSender.sendMessage(getLanguage().NoPower);
                return true;
            }
        }
        if (args.length<1){
            return super.command(commandSender, args);
        }else {
            Command command = commandMap.get(args[0].toUpperCase());
            if (command!=null){
                return command.command(commandSender, Arrays.copyOfRange(args,1,args.length));
            }else {
                if (commandImplement instanceof CommandImplementNoSet) {
                    commandSender.sendMessage(getLanguage().NoCommand);
                    return true;
                }else {
                    return super.command(commandSender,args);
                }
            }
        }

    }

    @Override
    public List<String> tabCompletion(CommandSender commandSender, String[] strings) {
        if (power!=null){
            if (!commandSender.hasPermission(power)) {
                return Collections.emptyList();
            }
        }
        List<String> list = new ArrayList<>();
        boolean noe = false;
        if (strings.length<1){
            list.addAll(commandStrings);
            noe = true;
        }else if(strings.length==1) {
            for (String commandString : commandStrings) {
                if (commandString.toUpperCase().startsWith(strings[0].toUpperCase())) {
                    list.add(commandString);
                }
            }
            noe = true;
        }else {
            Command command = commandMap.get(strings[0].toUpperCase());
            if (command!=null){
                List<String> tab = command.tabCompletion(commandSender,Arrays.copyOfRange(strings,1,strings.length));
                if (tab!=null){
                    list.addAll(tab);
                }
            }
        }
        if (noe){
            if (!(commandImplement instanceof CommandImplementNoSet)){
                List<String> tab =  super.tabCompletion(commandSender, strings);
                if (tab!=null){
                    list.addAll(tab);
                }
            }
        }
        return list;
    }


    CommandListLanguage getLanguage(){
        return (CommandListLanguage) language;
    }

    public CommandList addCommand(Command command){
        commands.add(command);
        return this;
    }

    @Override
    public CommandList setCommandImplement(CommandImplement commandImplement) {
        return (CommandList)super.setCommandImplement(commandImplement);
    }

    @Override
    public CommandList setAliases(List<String> aliases) {
        return (CommandList)super.setAliases(aliases);
    }

    @Override
    public CommandList setPower(String power) {
        return (CommandList)super.setPower(power);
    }

    @Override
    public void load(ConfigurationSectionSetDefGet configurationSectionSetDefGet) {
        super.load(configurationSectionSetDefGet);
        for (Command command : commands) {
            configurationSectionSetDefGet.getObjectSetDef("commands."+command.getMainCommand(),command);
        }
    }

    @Override
    public void first() {
        commandMap = new HashMap<>();
        commandStrings = new ArrayList<>();
        for (Command command : commands) {
            //运行初始化
            if (command instanceof NeedFirst){
                ((NeedFirst)command).first();
            }

            //自己初始化
            String mainCommand = command.getMainCommand();
            commandMap.put(mainCommand.toUpperCase(),command);
            commandStrings.add(mainCommand);
            List<String> aliases = command.getAliases();
            if (aliases!=null){
                for (String alias : aliases) {
                    commandMap.put(alias.toUpperCase(),command);
                    commandStrings.add(alias);
                }
            }
        }
    }
}
